package com.qingzhuge.member.controller;


import com.qingzhuge.common.validator.ValidatorUtil;
import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.core.annotation.security.Anonymous;
import com.qingzhuge.dto.response.BaseResponseDto;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.exception.LoginException;
import com.qingzhuge.member.dto.UserLoginRequest;
import com.qingzhuge.member.dto.UserLoginResponse;
import com.qingzhuge.member.dto.WxUserInfoRequestDto;
import com.qingzhuge.member.enums.EnumThirdOauthType;
import com.qingzhuge.member.service.ISysMemberService;
import com.qingzhuge.member.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author : zero.xiao
 * @description : 登录接口
 * @date :2018/6/14 0014 下午 17:29
 * @modified :
 * @since : 1.0.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "A会员登录")
public class ApiLoginController extends AbstractController {
    @Resource
    private ISysMemberService memberService;
    @Resource
    private IWxUserService wxUserService;

    @ApiOperation("微信小程序登陆")
    @PostMapping("/login/{type}/{appid}")
    public ResponseEntity<ResponseBodyDto<UserLoginResponse>> login(@PathVariable String appid,
                                                         @RequestBody WxUserInfoRequestDto request,
                                                         @PathVariable EnumThirdOauthType type) {
        if (null == appid || null == type || null == request || StringUtils.isEmpty(request.getCode())){
            throw new LoginException();
        }
        UserLoginResponse response = wxUserService.login(appid, request, type);
        return returnSuccess(response);
    }

    @Anonymous
    @PostMapping("login")
    @ApiOperation("登录")
    public ResponseEntity<ResponseBodyDto<BaseResponseDto>> login(@RequestBody UserLoginRequest request) {
        //表单校验
        ValidatorUtil.validateEntity(request);
        //用户登录
        BaseResponseDto response = memberService.login(request);
        return returnSuccess(response.getResCode(), response.getResMessage(), response);
    }

}
